package tgc.edu.demo.jkxt.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import tgc.edu.demo.jkxt.custom.AjaxResult;
import tgc.edu.demo.jkxt.custom.BaseController;
import tgc.edu.demo.jkxt.entity.Course;
import tgc.edu.demo.jkxt.entity.Knowledge;
import tgc.edu.demo.jkxt.form.KnowledgeForm;
import tgc.edu.demo.jkxt.service.CourseService;
import tgc.edu.demo.jkxt.service.KnowledgeService;

@RestController
@RequestMapping(value = "/zhishi")
public class KnowledgeController extends BaseController<Knowledge , Integer, KnowledgeForm> {
	@Autowired
	private KnowledgeService Knowledgeservice;
	@Autowired
	private CourseService courseService;
	
	@Override
	@RequestMapping(value = "/save")
	public Object save(KnowledgeForm form) {
		try {
			
		Integer id=form.getId();
		Knowledge know=new Knowledge();
		if(id!=null) {
			know=Knowledgeservice.findById(id);
		}
		Course course = courseService.findById(form.getCourseId());
		know.setCourse(course);
		know.setName(form.getName());
		Knowledgeservice.save(know);
		}catch (Exception e) {
			return new AjaxResult(false,"数据保存失败！");
		}
		return new AjaxResult("数据保存成功！");
	}
	
	@RequestMapping(value = "/findzhishi")
	public Object findKonwByCourseId(Integer courseId) {
		List<Knowledge> list = Knowledgeservice.findKonwledgeByCourseId(courseId);
		return new AjaxResult(list);
	}

	
}
